﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class GetStocksForDeliver : RibbonForm
    {
        public GetStocksForDeliver()
        {
            InitializeComponent();
        }

        public string ItemGuID { get; set; }
        public string DocumentNumber { get; set; }
        public string ItemCode { get; set; }
        public string ItemName { get; set; }
        public string UoM { get; set; }
        public string OrderQty { get; set; }
        public string PackSize { get; set; }

        public string ServedQty { get; set; }
        public string ServedPackSize { get; set; }

        public string WhsCode { get; set; }
        public string WhsName { get; set; }

        public string TotalServed { get; set; }

        public string ConnectionString { get; set; }

        bool IsNumeric(string _VAR_VALUE)
        {
            try
            {
                int _var = Convert.ToInt16(_VAR_VALUE);

                return true;
            }
            catch
            {
                return false;
            }
        }

        public DataTable DeliveredIssuedItems { get; set; }

        public DataTable InitializeIssuedItems()
        {
            DataTable _table = new DataTable();

            _table.Columns.Add("ItemCode", typeof(string));
            _table.Columns.Add("ItemName", typeof(string));
            _table.Columns.Add("QtySrvd", typeof(string));
            _table.Columns.Add("UoM", typeof(string));
            _table.Columns.Add("UnitCost", typeof(string));
            _table.Columns.Add("LotNumber", typeof(string));
            _table.Columns.Add("ItemGuID", typeof(string));

            return _table;
        }

        void BindAvailableStocks()
        {
            StockManager oManager = new StockManager();
            DataTable _table = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            _table = oManager.GetAvailableStockByItemCode(this.WhsCode, this.ItemCode);

            foreach (DataRow oRow in _table.Rows)
            {
                foreach (DataRow dRow in this.DeliveredIssuedItems.Rows)
                {
                    if (dRow.RowState != DataRowState.Deleted)
                    {
                        if (oRow["ItemCode"].ToString() == dRow["ItemCode"].ToString() && oRow["LotNumber"].ToString() == dRow["LotNumber"].ToString())
                        {
                            oRow["Qty"] = Convert.ToDouble(oRow["Qty"]) - Convert.ToDouble(dRow["QtySrvd"]);
                        }
                    }
                }
            }

            _table.AcceptChanges();
            gridControl1.DataSource = _table;
            gridControl1.Refresh();

            oManager.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            double _total = 0;

            foreach (DataRow oRow in this.DeliveredIssuedItems.Rows)
            {
                if (oRow.RowState != DataRowState.Deleted)
                {
                    if (this.ItemGuID == oRow["ItemGuID"].ToString().Trim())
                    {
                        _total = _total + Convert.ToDouble(oRow["QtySrvd"]);

                    }
                }
            }


            if (this.DeliveredIssuedItems.Rows.Count > 0)
            {
                int iCountItem = 0;

                foreach (DataRow oRow in this.DeliveredIssuedItems.Rows)
                {
                    if (oRow.RowState != DataRowState.Deleted)
                    {
                        if (oRow["ItemGuID"].ToString() == this.ItemGuID)
                        {
                            iCountItem = iCountItem + 1;
                        }
                    }
                }
                if (iCountItem > 0)
                {
                    this.TotalServed = Convert.ToDouble(txtQtyOrder.Text).ToString();
                }
                else
                {
                    this.TotalServed = "0.00";
                }
            }
            else
            {
                this.TotalServed = "0.00";
            }
            
            
            this.Close();
        }

        private void GetStocksForDeliver_Load(object sender, EventArgs e)
        {
            txtWarehouse.Text = this.WhsName;
            txtItemcode.Text = this.ItemCode;
            txtDescription.Text = this.ItemName;
            txtUoM.Text = this.UoM;

            if (Convert.ToDouble(this.ServedQty) == 0)
            {
                txtPacksize.Text = this.PackSize;
                txtQtyOrder.Text = this.OrderQty;
                
                double iQty = Convert.ToDouble(txtQtyOrder.Text);
                double iPackSize = Convert.ToDouble(txtPacksize.Text);
                double iTotalQty = iQty * iPackSize;

                txtTotalQty.Text = iTotalQty.ToString();
            }
            else
            {
                txtPacksize.Text = this.ServedPackSize;
                txtQtyOrder.Text = this.ServedQty;

                double iQty = Convert.ToDouble(txtQtyOrder.Text);
                double iPackSize = Convert.ToDouble(txtPacksize.Text);
                double iTotalQty = iQty * iPackSize;

                txtTotalQty.Text = iTotalQty.ToString();


            }

            BindAvailableStocks();

            gridControl2.DataSource = this.DeliveredIssuedItems;
            gridControl2.Refresh();

            gridView2.FindFilterText = this.ItemGuID;
            
        }

        private void txtPickItem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

            if (oButtonEdit.Text != "" && IsNumeric(oButtonEdit.Text))
            {
                int iCount = -1;
                int[] oRow = gridView1.GetSelectedRows();

                iCount = oRow[0];

                double _QtySrvd = Convert.ToDouble(oButtonEdit.Text);
                double _AvailableQty = Convert.ToDouble(gridView1.GetRowCellValue(iCount, gridView1.Columns["Qty"]));
                string _UnitCost = gridView1.GetRowCellValue(iCount, gridView1.Columns["UnitCost"]).ToString();
                
                if (_QtySrvd>_AvailableQty)
                {
                    MessageBox.Show("Please enter not less than to the available quantity!!!", "On-Hand", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    string _LOT_NUMBER = gridView1.GetRowCellValue(iCount, gridView1.Columns["LotNumber"]).ToString();

                    gridView1.SetRowCellValue(iCount, gridView1.Columns["Qty"], _AvailableQty - _QtySrvd);
                    gridView1.SetRowCellValue(iCount, gridView1.Columns["PickQty"], 0);

                    DataRow oRowItem = this.DeliveredIssuedItems.NewRow();

                    oRowItem["ItemCode"] = txtItemcode.Text;
                    oRowItem["ItemName"] = txtDescription.Text;
                    oRowItem["QtySrvd"] = _QtySrvd;
                    oRowItem["UnitCost"] = _UnitCost;
                    oRowItem["UoM"] = txtUoM.Text;
                    oRowItem["IsFree"] = false;
                    oRowItem["LotNumber"] = _LOT_NUMBER;
                    oRowItem["ItemGuID"] = this.ItemGuID;


                    this.DeliveredIssuedItems.Rows.Add(oRowItem);
                    this.DeliveredIssuedItems.AcceptChanges();
                    gridControl2.DataSource = this.DeliveredIssuedItems;
                    gridControl2.Refresh();
                }
            }

            

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            double _total = 0;

            foreach (DataRow oRow in this.DeliveredIssuedItems.Rows)
            {
                if (oRow.RowState != DataRowState.Deleted)
                {
                    if (this.ItemGuID == oRow["ItemGuID"].ToString().Trim())
                    {
                        _total = _total + Convert.ToDouble(oRow["QtySrvd"]);

                    }
                }
            }

            if (this.DeliveredIssuedItems.Rows.Count > 0)
            {
                int iCountItem = 0;

                foreach (DataRow oRow in this.DeliveredIssuedItems.Rows)
                {
                    if (oRow.RowState != DataRowState.Deleted)
                    {
                        if (oRow["ItemGuID"].ToString() == this.ItemGuID)
                        {
                            iCountItem = iCountItem + 1;
                        }
                    }
                }
                if (iCountItem > 0)
                {
                    this.TotalServed = Convert.ToDouble(txtQtyOrder.Text).ToString();
                }
                else
                {
                    this.TotalServed = "0.00";
                }
            }
            else
            {
                this.TotalServed = "0.00";
            }
            
            this.Close();
        }

        private void txtPickItem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DevExpress.XtraEditors.ButtonEdit oButtonEdit = (DevExpress.XtraEditors.ButtonEdit)sender;

                if (oButtonEdit.Text != "" && IsNumeric(oButtonEdit.Text))
                {
                    int iCount = -1;
                    int[] oRow = gridView1.GetSelectedRows();

                    iCount = oRow[0];

                    double _QtySrvd = Convert.ToDouble(oButtonEdit.Text);
                    double _AvailableQty = Convert.ToDouble(gridView1.GetRowCellValue(iCount, gridView1.Columns["Qty"]));
                    string _LOT_NUMBER = gridView1.GetRowCellValue(iCount, gridView1.Columns["LotNumber"]).ToString();
                    string _UnitCost = gridView1.GetRowCellValue(iCount, gridView1.Columns["UnitCost"]).ToString();
                    
                    if (_QtySrvd > _AvailableQty)
                    {
                        MessageBox.Show("Please enter not less than to the available quantity!!!", "On-Hand", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        gridView1.SetRowCellValue(iCount, gridView1.Columns["Qty"], _AvailableQty - _QtySrvd);
                        gridView1.SetRowCellValue(iCount, gridView1.Columns["PickQty"], 0);

                        DataRow oRowItem = this.DeliveredIssuedItems.NewRow();

                        oRowItem["ItemCode"] = txtItemcode.Text;
                        oRowItem["ItemName"] = txtDescription.Text;
                        oRowItem["QtySrvd"] = _QtySrvd;
                        oRowItem["UnitCost"] = _UnitCost;
                        oRowItem["UoM"] = txtUoM.Text;
                        oRowItem["LotNumber"] = _LOT_NUMBER;
                        oRowItem["ItemGuID"] = this.ItemGuID;

                        this.DeliveredIssuedItems.Rows.Add(oRowItem);
                        this.DeliveredIssuedItems.AcceptChanges();
                        gridControl2.DataSource = this.DeliveredIssuedItems;
                        gridControl2.Refresh();
                    }
                }

            }
        }

        private void gridControl2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int iCount = -1;
                int[] oRow = gridView2.GetSelectedRows();

                iCount = oRow[0];
            
                gridView2.DeleteRow(iCount);
                gridView2.RefreshData();

                BindAvailableStocks();
            }
        }

        private void txtQtyOrder_EditValueChanged(object sender, EventArgs e)
        {
            
        }

        private void txtQtyOrder_Leave(object sender, EventArgs e)
        {
            double iQty = Convert.ToDouble(txtQtyOrder.Text);
            double iPackSize = Convert.ToDouble(txtPacksize.Text);
            double iTotalQty = iQty * iPackSize;

            txtTotalQty.Text = iTotalQty.ToString();
        }

        private void btnAutoPick_Click(object sender, EventArgs e)
        {
            double iTotalQty = 0;
            double _TotalQty = 0;
            double iAppliedQty = 0;

            DataTable _dtAvailStocks;

            iTotalQty = Convert.ToDouble(txtTotalQty.Text);
            _TotalQty = Convert.ToDouble(txtTotalQty.Text);
            StockManager oManager = new StockManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            _dtAvailStocks = oManager.GetAvailableStockByItemCode(this.WhsCode, this.ItemCode);
            oManager.Close();


            foreach(DataRow oRow in _dtAvailStocks.Rows)
            {
                if (iAppliedQty < _TotalQty)
                {
                    if (iTotalQty <= Convert.ToDouble(oRow["Qty"]))
                    {
                        oRow["Qty"] = Convert.ToDouble(oRow["Qty"]) - iTotalQty;

                        iAppliedQty = iAppliedQty + iTotalQty;

                        DataRow oRowItem = this.DeliveredIssuedItems.NewRow();
                        oRowItem["ItemCode"] = txtItemcode.Text;
                        oRowItem["ItemName"] = txtDescription.Text;
                        oRowItem["QtySrvd"] = iTotalQty;
                        oRowItem["UnitCost"] = oRow["UnitCost"].ToString();
                        oRowItem["UoM"] = oRow["UoM"].ToString();
                        oRowItem["LotNumber"] = oRow["LotNumber"].ToString();
                        oRowItem["ItemGuID"] = this.ItemGuID;

                        this.DeliveredIssuedItems.Rows.Add(oRowItem);
                        this.DeliveredIssuedItems.AcceptChanges();
                    }
                    else
                    {

                        iAppliedQty = Convert.ToDouble(oRow["Qty"].ToString());
                        oRow["Qty"] = 0;

                        DataRow oRowItem = this.DeliveredIssuedItems.NewRow();
                        oRowItem["ItemCode"] = txtItemcode.Text;
                        oRowItem["ItemName"] = txtDescription.Text;
                        oRowItem["QtySrvd"] = iAppliedQty;
                        oRowItem["UoM"] = oRow["UoM"].ToString();
                        oRowItem["UnitCost"] = oRow["UnitCost"].ToString();
                        oRowItem["LotNumber"] = oRow["LotNumber"].ToString();
                        oRowItem["ItemGuID"] = this.ItemGuID;

                        iTotalQty = iTotalQty - iAppliedQty;
                        this.DeliveredIssuedItems.Rows.Add(oRowItem);
                        this.DeliveredIssuedItems.AcceptChanges();
                    }
                }
                else
                {
                    break;
                }

            }

            if (iAppliedQty != _TotalQty)
            {
                List<DataRow> lRows = new List<DataRow>();

                foreach (DataRow _rows in this.DeliveredIssuedItems.Rows)
                {
                    if (_rows.RowState != DataRowState.Deleted)
                    {
                        if (_rows["ItemGuID"].ToString() == this.ItemGuID)
                        {
                            lRows.Add(_rows);
                        }
                    }
                }

                foreach (DataRow _delRow in lRows)
                {
                    this.DeliveredIssuedItems.Rows.Remove(_delRow);
                }

                this.DeliveredIssuedItems.AcceptChanges();
                BindAvailableStocks();

                MessageBox.Show("Items requested are not supplied because of insufficient inventory!!!", "Inventory", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {

                gridControl1.DataSource = _dtAvailStocks;
                gridControl1.Refresh();

                gridControl2.DataSource = this.DeliveredIssuedItems;
                gridControl2.Refresh();
            }


        }
    }
}
